require(["common", "jQuery", "coAjax", "queryString", "href", "hash_routie"], function(jSouper, $, coAjax, QueryString, href, hash_routie) {
	var _num = 10;
	var _page = 0;

	hash_routie.on("bina", function() {
		_init_goods_data();
		_init_bina_goods();
		_init_bina_user();
	});
	//分页功能
	function _load_goods_page(num, page) {
		coAjax.get(appConfig.goods.list_with_page_info, {
			bus_id: busInfo._id,
			num: num,
			page: page
		}, function(result) {
			var info = result.result;
			console.log("bus.goods", info.goods);
			App.set("goods", info.goods);
			var page_info = {
				total_num: info.total_num,
				total_page: info.total_page,
				page: info.page,
				num: info.num
			};
			page_info._number_list = [];
			page_info._number_list.length = info.total_page;
			App.set("$Cache.bus.goods.page_info", page_info);
		});
	};

	//获取商品列表
	function _init_goods_data() {
		var queryString = new QueryString(location.hash);
		_num = queryString.get("num") || _num;
		_page = queryString.get("page") || _page;
		_load_goods_page(_num, _page);
	};
	//获取购买二进制商品的用户列表
	function _init_bina_user() {
		coAjax.get(appConfig.bus.extend.bina.user, function(result) {
			App.set("$Cache.bina.user_list", result.result);
		});
	};
	//获取商品二进制配置
	function _init_bina_config() {
		coAjax.get(appConfig.bus.extend.bina.config, function(result) {
			App.set("$Cache.bina_buff", result.result);
		})
	};
	_init_bina_config();

	//获取二进制商品信息
	function _init_bina_goods() {
		coAjax.get(appConfig.bus.extend.bina.goods, function(result) {
			App.set("$Cache.bina.bina_list", result.result);
		});
	};

	//设定商品为二进制商品
	App.set("$Event.set_as_bina_goods", function(e, vm) {
		var goods_id_list = jSouper.map(App.get("$Cache.bina.bina_list"), function(bina_info) {
			return bina_info.goods._id;
		});
		goods_id_list.push(vm.get("_id"));
		vm.set("is_bina", true);
		coAjax.put(appConfig.bus.extend.bina.goods, {
			goods_id_list: goods_id_list
		}, function(result) {
			App.set("$Cache.bina.bina_list", result.result);
			_init_goods_data();
		});
	});
	//取消指定商品的二进制设定
	App.set("$Event.cancel_as_bina_goods", function(e, vm) {
		var goods_id_list = jSouper.map(App.get("$Cache.bina.bina_list"), function(bina_info) {
			return bina_info.goods._id;
		});
		var remover_id = vm.get("goods._id");
		goods_id_list = jSouper.filter(goods_id_list, function(goods_id) {
			return remover_id !== goods_id
		});
		coAjax.put(appConfig.bus.extend.bina.goods, {
			goods_id_list: goods_id_list
		}, function(result) {
			App.set("$Cache.bina.bina_list", result.result);
			_init_goods_data();
		});
	});
	//设定独立商品的分销比例
	App.set("$Event.open_edit_goods_bina_config", function(e, vm) {
		vm.set("$Private.$Cache.show_setting", true);
	});
	App.set("$Event.close_edit_goods_bina_config", function(e, vm) {
		vm.set("$Private.$Cache.show_setting", false);
	});
	App.set("$Event.use_custom_bina_id.toggle", function(e, vm) {
		if (vm.get("goods.use_custom_bina_id")) {
			App.get("$Event.set_custom_bina_config")(e, vm);
		} else {
			coAjax["delete"](appConfig.bus.extend.bina.remove_goods_custom_bina + vm.get("goods._id"), function(result) {
				vm.set(result.result);
			});
		}
	});
	App.set("$Event.set_custom_bina_config", function(e, vm) {
		var buff_config = vm.get("goods_custom_bina_buff.config") || {};
		buff_config.first = parseFloat(buff_config.first) || 0;
		buff_config.second = parseFloat(buff_config.second) || 0;
		buff_config.third = parseFloat(buff_config.third) || 0;
		coAjax.put(appConfig.bus.extend.bina.set_goods_custom_bina + vm.get("goods._id"), buff_config, function(result) {
			vm.set(result.result);
		});
	});

	//兑换确认打款
	App.set("$Event.bina_申请兑换中_to_商家已付款", function(e, vm) {
		coAjax.put(appConfig.bus.extend.bina.申请兑换中_to_商家已付款, {
			user_id: vm.get("user_id")
		}, function(result) {
			vm.set(result.result);
		});
	});
	//修改商品二进制设置
	App.set("$Event.bina_buff.edit", function() {
		App.set("$Cahcs.bina_buff.editing", true);
	});
	App.set("$Event.bina_buff.upload", function() {
		var new_config = App.get("$Cache.bina_buff.new_config");
		var old_config = App.get("$Cache.bina_buff.config");
		if (!new_config) {
			App.get("$Event.bina_buff.cancle")();
			return;
		}
		coAjax.put(appConfig.bus.extend.bina.config, {
			first: new_config.first || old_config.first,
			second: new_config.second || old_config.second,
			third: new_config.third || old_config.third
		}, function(result) {
			App.set("$Cache.bina_buff", result.result);
			App.get("$Event.bina_buff.cancle")();
		});
	});
	App.set("$Event.bina_buff.cancle", function() {
		App.set("$Cahcs.bina_buff.editing", false);
		App.set("$Cache.bina_buff.new_config", null);
	});
});